# main.py
import logging
from logger_config import app_logger

from blog_manager import BlogManager

loger = logging.getLogger('app.main')

def main():
    manager = BlogManager(database_file_path := 'data/blog.db')
    loger.debug(f'联结数据库文件 {database_file_path}')

    while True:
        loger.debug('进入选择菜单')
        print("\n=== 个人博客系统 ===")
        print("1. 创建用户")
        print("2. 发布文章")
        print("3. 查看用户文章")
        print("4. 退出")

        choice = input("请选择操作: ")

        if choice == '1':
            loger.debug('用户创建')
            # 用户创建逻辑
            username = input("用户名: ")
            email = input("邮箱: ")
            if manager.create_user(username, email):
                print("用户创建成功!")
                loger.info(f'用户{username}创建成功')

        elif choice == '2':
            # 文章发布逻辑
            title = input("文章标题: ")
            content = input("文章内容: ")
            author_id = input("作者ID: ")
            try:
                author_id = int(author_id)
                if manager.user_is_exist(author_id):
                    if manager.create_post(title, content, author_id):
                        print("文章发布成功!")
                else:
                    print("文章发布失败，请检查作者ID是否存在")
            except ValueError:
                print("作者ID必须是数字")

        elif choice == '3':
            # 查看用户文章逻辑
            author_id = input("作者ID或者用户名: ")
            try:
                author_id = int(author_id)
                posts = manager.get_posts_by_author(author_id)
                if posts:
                    print(f"作者ID为{author_id}的用户发布的文章:")
                    for post in posts:
                        print(f"标题: {post['title']}")
                        print(f"内容: {post['content']}")

            except ValueError:
                author_name = author_id
                posts = manager.get_posts_by_author(author_name)
                if posts:
                    print(f"作者名为{author_name}的用户发布的文章:")
                    for post in posts:
                        print(f"标题: {post['title']}")
                        print(f"内容: {post['content']}")

        elif choice == '4':
            break

if __name__ == '__main__':
    main()